package com.job.meeting.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.job.meeting.tools.wechatSystem;
import com.job.meeting.pojo.*;
import com.job.meeting.service.*;

@Controller
public class baseController {
    @Autowired
    userService service;

    private enum TYPE{
        BOOKING, MEETING_ROOM, MANAGEMENT
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private class redirectValue{
        String url;
        user user;
    }


    //用于初始化，根据微信授权页面重定向获得的code和state，来规定重定向。state中，0是预约，1会议室，2是个人
    @GetMapping("/base")
    public String init(@RequestParam("code") String code, @RequestParam("state") int state){
        String result = "redirect:";
        user u = new wechatSystem(service).getWechatUser(code);
        if(service.updateUser(u) == 0)
        {
            service.insertUser(u);
        }
        TYPE t = TYPE.values()[state];
        redirectValue value = new redirectValue();
        switch (t){
            case BOOKING:
                result += ("/booking/page?openid=" + u.getOpenid());
                break;
            case MEETING_ROOM:
                result += ("/meetingRoom/page?openid=" + u.getOpenid());
                break;
            case MANAGEMENT:
                result += ("/management/page?openid=" + u.getOpenid());
                break;
            default:
                break;
        }
        return result;
    }
}